﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using MedicService.Domain.Print;
using MedicService.Domain.Print.Template;
using MedicService.Server.Exception;
using MedicService.Server.Factory.Generic;

namespace MedicService.Server.Factory.Print {
    public class ActiveTemplateFactory : EntityFactory<ActiveTemplate> {
        public ActiveTemplateFactory(string where) : base("PrintActiveTemplate", "Id", where) { }
        public ActiveTemplateFactory() : base("PrintActiveTemplate") { }
        protected override ActiveTemplate CreateEntity() {
            var activeTemplate = new ActiveTemplate();
            return activeTemplate;
        }

        protected override string GetInsertQuery(ActiveTemplate entity) {
            int OrderTemplateId = 0;
            int ExtractTemplateId = 0;
            int InvoiceTemplateId = 0;
            int SertificateTemplateId = 0;
            int AttestatProtocolTemplateId = 0;
            int ProfileProtocolTemplateId = 0;
            try {
                try {
                    OrderTemplateId = entity.OrderTemplate.Id;
                } finally {
                    try {
                        ExtractTemplateId = entity.ExtractTemplate.Id;
                    } finally {
                        try {
                            InvoiceTemplateId = entity.InvoiceTemplate.Id;
                        } finally {
                            try {
                                SertificateTemplateId = entity.SertificateTemplate.Id;
                            } finally {
                                try {
                                    AttestatProtocolTemplateId = entity.AttestatProtocolTemplate.Id;
                                } finally {
                                    try {
                                        ProfileProtocolTemplateId = entity.ProfileProtocolTemplate.Id;
                                    } finally { }
                                }
                            }
                        }
                    }
                }
            } catch (PrintTemplateException e) {

            }
            string q = "INSERT INTO [PrintActiveTemplate]"
                       + "           ([OrderTemplateId]"
                       + "           ,[ExtractTemplateId]"
                       + "           ,[InvoiceTemplateId]"
                       + "           ,[SertificateTemplateId]"
                       + "           ,[AttestatProtocolTemplateId]"
                       + "           ,[ProfileProtocolTemplateId])"
                       + "     VALUES"
                       + "           (" + OrderTemplateId
                       + "           ," + ExtractTemplateId
                       + "           ," + InvoiceTemplateId
                       + "           ," + SertificateTemplateId
                       + "           ," + AttestatProtocolTemplateId
                       + "           ," + ProfileProtocolTemplateId + ")";
            return q;
        }

        protected override string GetUpdateQuery(ActiveTemplate entity) {
            int OrderTemplateId = 0;
            int ExtractTemplateId = 0;
            int InvoiceTemplateId = 0;
            int SertificateTemplateId = 0;
            int AttestatProtocolTemplateId = 0;
            int ProfileProtocolTemplateId = 0;
            try {
                try {
                    OrderTemplateId = entity.OrderTemplate.Id;
                } finally {
                    try {
                        ExtractTemplateId = entity.ExtractTemplate.Id;
                    } finally {
                        try {
                            InvoiceTemplateId = entity.InvoiceTemplate.Id;
                        } finally {
                            try {
                                SertificateTemplateId = entity.SertificateTemplate.Id;
                            } finally {
                                try {
                                    AttestatProtocolTemplateId = entity.AttestatProtocolTemplate.Id;
                                } finally {
                                    try {
                                        ProfileProtocolTemplateId = entity.ProfileProtocolTemplate.Id;
                                    } finally {}
                                }
                            }
                        }
                    }
                }
            }catch(PrintTemplateException e) {
                
            }
            string q = "UPDATE [PrintActiveTemplate]"
                       + "   SET [OrderTemplateId] = " + OrderTemplateId
                       + "      ,[ExtractTemplateId] = " + ExtractTemplateId
                       + "      ,[InvoiceTemplateId] = " + InvoiceTemplateId
                       + "      ,[SertificateTemplateId] = " + SertificateTemplateId
                       + "      ,[AttestatProtocolTemplateId] = " + AttestatProtocolTemplateId
                       + "      ,[ProfileProtocolTemplateId] = " + ProfileProtocolTemplateId
                       + " WHERE id=" + entity.Id;
            return q;
        }

        public ActiveTemplate GetActive() {
            //STUB
            return Load(1);
            //ENDSTUB
        }

        protected override void FillEntity(ActiveTemplate entity, DataRow myDataRow) {
            base.FillEntity(entity, myDataRow);
            entity.AttestatProtocolTemplate = new PrintAttestatProtocolTemplateFactory().Load(myDataRow["AttestatProtocolTemplateId"]);
            entity.ExtractTemplate = new PrintExtractTemplateFactory().Load(myDataRow["ExtractTemplateId"]);
            entity.InvoiceTemplate = new PrintTemplateInvoiceFactory().Load(myDataRow["InvoiceTemplateId"]);
            entity.SertificateTemplate = new PrintSertificateTemplateFactory().Load(myDataRow["SertificateTemplateId"]);
            entity.OrderTemplate = new PrintOrderTemplateFactory().Load(myDataRow["OrderTemplateId"]);
            entity.ProfileProtocolTemplate = new PrintProfileProtocolTemplateFactory().Load(myDataRow["ProfileProtocolTemplateId"]);
        }

    }
}